#include "E_Transform.h"
#include <cmath>

void E_Transform::SetMatrix(E_Point* P, int Pt)
{
	this->P = P;
	PtNumber = Pt;
}

void E_Transform::E_Translate(double tx, double ty)
{
	Identity();
	T[0][2] = tx; T[1][2] = ty;
	MultiplyMatrix();
}

void E_Transform::E_Rotate(double thet)
{
	Identity();
	T[0][0] = cos(thet); T[0][1] = -sin(thet);
	T[1][0] = sin(thet); T[1][1] = cos(thet);
	MultiplyMatrix();
}

void E_Transform::E_Rotate(double thet, E_Point P)
{
	E_Translate(-P.x, -P.y);
	E_Rotate(thet);
	E_Translate(P.x, P.y);
}

void E_Transform::E_Scale(double dx, double dy)
{
	Identity();
	T[0][0] = dx; T[1][1] = dy;
	MultiplyMatrix();
}

void E_Transform::E_ReflectO()
{
	Identity();
	T[0][0] = -1; T[1][1] = -1;
	MultiplyMatrix();
}

void E_Transform::E_ReflectX()
{
	Identity();
	T[0][0] = 1; T[1][1] = -1;
	MultiplyMatrix();
}

void E_Transform::E_ReflectY()
{
	Identity();
	T[0][0] = -1; T[1][1] = 1;
	MultiplyMatrix();
}

void E_Transform::E_Shear(double b,double c)
{
	Identity();
	T[0][1] = b; T[1][0] = c;
	MultiplyMatrix();
}

void E_Transform::Identity()
{
	T[0][0] = 1.0; T[0][1] = 0.0; T[0][2] = 0.0;
	T[1][0] = 0.0; T[1][1] = 1.0; T[1][2] = 0.0;
	T[2][0] = 0.0; T[2][1] = 0.0; T[2][2] = 1.0;
}

void E_Transform::MultiplyMatrix()
{
	E_Point* pNew = new E_Point[PtNumber];
	for (int j = 0; j < PtNumber; j++) {
		pNew[j] = P[j];
	}
	for (int i = 0; i < PtNumber; i++) {
		P[i].x = pNew[i].x * T[0][0] + pNew[i].y * T[0][1] + pNew[i].w * T[0][2];
		P[i].y = pNew[i].x * T[1][0] + pNew[i].y * T[1][1] + pNew[i].w * T[1][2];
		P[i].w = pNew[i].x * T[2][0] + pNew[i].y * T[2][1] + pNew[i].w * T[2][2];
	}
	delete[]pNew;
}
